﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using VPBS.Common;

namespace VPBS.SqlClient.DAL
{
    public class TnPlusCallSmsDAL
    {
        /// <summary>
        /// Lay thong tin Ratio ban ghi gan nhat cu 1 tai khoan
        /// </summary>
        /// <param ></param>
        /// <returns>Get_RatioOld</returns>
        public static decimal Get_RatioOld(string AccountNo)
        {
            try
            {
                string command =
                    @"Select top(1) Ratio From CallSMS Where AccountNo='" + AccountNo + "' And Datediff(Day,PostDate,Getdate()) = 0 Order by PostDate desc";

                DataSet ds = SqlHelper.ExecuteDataset(ConnectStringHelper.TnPlusConnectionString, CommandType.Text,
                                                      command);
                if (ds != null && ds.Tables.Count > 0)
                    return decimal.Parse(ds.Tables[0].Rows[0][0].ToString());
                return 0M;
            }
            catch (Exception ex)
            {
                //Logger.Error(typeof(Get_RatioOld), ex);
                return 0M;
            }
        }

        /// <summary>
        /// Update trạng thái Status khi gửi thành công tin nhắn call
        /// </summary>
        /// <param value="IdCallSMS" ></param>
        /// /// <param value="Status" ></param>
        /// <returns>Bool</returns>
        public static void Update_Status(string IdCallSMS, string Status)
        {
            try
            {
                string command = @"Update CallSMS set Status='" + Status + "' Where IdCallSMS='" + IdCallSMS + "'";

                SqlHelper.ExecuteNonQuery(ConnectStringHelper.TnPlusConnectionString, CommandType.Text, command);
            }
            catch (Exception ex)
            {
                //Logger.Error(typeof(Update_Status), ex);
            }
        }

        /// <summary>
        /// Lấy danh sách cảnh báo chờ gửi đi
        /// </summary>
        /// <param ></param>
        /// <returns>DataTable </returns>
        public static DataTable Select_WaitToSendSMSLst()
        {
            try
            {
                string command = "Select * From CallSMS Where Datediff(Day, PostDate, Getdate()) = 0 And Status is Null";

                DataSet ds = SqlHelper.ExecuteDataset(ConnectStringHelper.TnPlusConnectionString, CommandType.Text,
                                                      command);
                if (ds != null && ds.Tables.Count > 0)
                    return ds.Tables[0];
                return new DataTable();
            }
            catch (Exception ex)
            {
                //Logger.Error(typeof(Select_WaitToSendSMSLst), ex);
                return new DataTable();
            }
        }

        /// <summary>
        /// Lay CallSMS theo IdCommon
        /// </summary>
        /// <param value="IdCommon" ></param>
        /// <returns>DataTable Get_CallSMS_With_IdCommon</returns>
        public static DataTable Get_CallSMS_With_IdCommon(string IdCommon)
        {
            try
            {
                string command =
                    @"select * from CallSMS where datediff(day,PostDate,Getdate())=0 and IdCommon='" + IdCommon +
                    "'";

                DataSet ds = SqlHelper.ExecuteDataset(ConnectStringHelper.TnPlusConnectionString, CommandType.Text,
                                                      command);
                if (ds != null && ds.Tables.Count > 0)
                    return ds.Tables[0];
                return new DataTable();
            }
            catch (Exception ex)
            {
                //Logger.Error(typeof(Get_CallSMS_With_IdCommon), ex);
                return new DataTable();
            }
        }

        /// <summary>
        /// Lay CallSMS trong ngay
        /// </summary>
        /// <param value="IdCommon" ></param>
        /// <returns>DataTable Get_CallSMSToday</returns>
        public static DataTable Get_CallSMSToday()
        {
            try
            {
                string command =
                    @"select distinct IdCommon from CallSMS where datediff(day,PostDate,Getdate())=0";

                DataSet ds = SqlHelper.ExecuteDataset(ConnectStringHelper.TnPlusConnectionString, CommandType.Text,
                                                      command);
                if (ds != null && ds.Tables.Count > 0)
                    return ds.Tables[0];
                return new DataTable();
            }
            catch (Exception ex)
            {
                //Logger.Error(typeof(Get_CallSMSToday), ex);
                return new DataTable();
            }
        }
    }
}
